package com.liuzhilong.infrastructure.framework.netty.standard.server;

import com.liuzhilong.infrastructure.framework.netty.api.standard.command.Command;
import com.liuzhilong.infrastructure.framework.netty.api.standard.command.CommandType;
import com.liuzhilong.infrastructure.framework.netty.api.standard.processer.AbstractProcessor;
import io.netty.channel.ChannelHandlerContext;

import java.io.UnsupportedEncodingException;

/**
 * 简单的输出
 */
public class EchoService extends AbstractProcessor<String> {


    @Override
    protected String parse(Command command) {
        try {
            return new String(command.getBody(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 输出完了之后，回复收到
     * @param channelHandlerContext
     * @param s
     */
    @Override
    protected void doProcessor(ChannelHandlerContext channelHandlerContext, String s) {
        System.out.println("接收到客户端消息"+s);
        try {
            channelHandlerContext.writeAndFlush(Command.create(CommandType.ECHO,"收到".getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
